const fp = require('lodash/fp')
const cars = [{
  name: 'Ferrari FF',
  horsepower: 660,
  dollar_value: 700000,
  in_stock: true
},
  {
    name: 'Spyker C12 Zagato',
    horsepower: 650,
    dollar_value: 648000,
    in_stock: false
  },
  {
    name: 'Jaguar XKR-S',
    horsepower: 550,
    dollar_value: 132000,
    in_stock: false
  },
  {
    name: 'Audi R8',
    horsepower: 625,
    dollar_value: 114200,
    in_stock: false
  },
  {
    name: 'Aston Martin One-7',
    horsepower: 750,
    dollar_value: 1850000,
    in_stock: true
  },
  {
    name: 'Pagani Huayara',
    horsepower: 700,
    dollar_value: 1300000,
    in_stock: true
  }
]

// 练习一
let last_car = cars => fp.last ( cars )
let last_prop = last => fp.prop ( 'in_stock', last )
let last_cars_prop = fp.flowRight(last_prop, last_car)
console.log(last_cars_prop(cars))

// 练习二
let first_car = cars => fp.first ( cars )
let first_prop = first => fp.prop ( 'name', first )
let first_cars_prop = fp.flowRight(first_prop, first_car)
console.log(first_cars_prop(cars))

// 练习三
let _average = function ( xs ) {
  return fp.reduce( fp.add,0, xs) /xs.length
}
let averageDollar = function (cars){
  let dollar_values = fp.map ( car => car.dollar_value, cars)
  return dollar_values
}
let averageDollarValue = fp.flowRight(_average, averageDollar)
console.log(averageDollarValue(cars))

// 练习四
let _underscore = fp.replace( /\s+/g,'_')
let toUpper = str => fp.toUpper(str)
let sanitizeNames = fp.flowRight(toUpper, _underscore)
console.log(sanitizeNames('hello world'))
